﻿Imports BookOrderingSystem.BOSDataSetTableAdapters

Public Class index1
    Inherits System.Web.UI.Page


    Dim createdate As DateTime
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub gv_userCart_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv_userCart.RowDeleting
        Dim cartAdap As New ShoppingCartsTableAdapter
        Dim bsId = gv_userCart.Rows(e.RowIndex).Cells(0).Text()
        Dim bookstoreId As Integer = CInt(bsId)
        Dim id As String = gv_userCart.Rows(e.RowIndex).Cells(1).Text()
        Dim bookId As Integer = CInt(id)
        Dim adap As New UsersTableAdapter
        Dim userId = Session("UserId")
        cartAdap.Delete(userId, bookId, bookstoreId)
        Response.Redirect("~/ShoppingCart/index.aspx")
    End Sub

    Protected Sub gv_userCart_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles gv_userCart.DataBound

        'Dim usCartAdap As New UserCartTableAdapter
        'Dim bookstoreitemAdap As New BookstoreItemsTableAdapter
        'Dim userId = Session("UserId")
        'Dim usercartTable = usCartAdap.GetDataByUserId(userId)
        'For Each item As BookOrderingSystem.BOSDataSet.UserCartRow In usercartTable
        '    Dim bookid = item.BookId
        '    Dim bookstoreid = item.BookstoresId
        '    Dim bookprice = bookstoreitemAdap.GetDataByBookIdStoreId(bookstoreid, bookid)(0).Price
        '    Dim itemprice As Double = bookprice * item.BookQuantity
        '    totalprice += itemprice
        'Next
        'lb_TotalPrice.Text = "Total : " + totalprice.ToString()
    End Sub

    Protected Sub bn_Order_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bn_Order.Click
        Dim userId = Session("UserId")
        Dim orderAdap As New OrdersTableAdapter
        createdate = DateTime.Now()
        'orderAdap.Insert(
        Dim totalprice = Session("TotalPrice")
        orderAdap.Insert(createdate, totalprice, userId)

        Dim usCartAdap As New UserCartTableAdapter
        Dim usercartTable = usCartAdap.GetDataByUserId(userId)
        Dim orderLinesAdap As New OrderLinesTableAdapter
        For Each item As BookOrderingSystem.BOSDataSet.UserCartRow In usercartTable
            Dim orderid = orderAdap.GetDataByUserAmountOrderDate(createdate, totalprice, userId)(0).OrderId
            orderLinesAdap.Insert(orderid, item.BookstoresId, item.BookId)
        Next



    End Sub

    Protected Sub gv_userCart_DataBinding(ByVal sender As Object, ByVal e As EventArgs) Handles gv_userCart.DataBinding
        Dim totalprice As Double
        Dim usCartAdap As New UserCartTableAdapter
        Dim bookstoreitemAdap As New BookstoreItemsTableAdapter
        Dim userId = Session("UserId")
        Dim usercartTable = usCartAdap.GetDataByUserId(userId)
        For Each item As BookOrderingSystem.BOSDataSet.UserCartRow In usercartTable
            Dim bookid = item.BookId
            Dim bookstoreid = item.BookstoresId
            Dim bookprice = bookstoreitemAdap.GetDataByBookIdStoreId(bookstoreid, bookid)(0).Price
            Dim itemprice As Double = bookprice * item.BookQuantity
            totalprice += itemprice
        Next
        Session("TotalPrice") = totalprice
        lb_TotalPrice.Text = "Total : " + totalprice.ToString()
    End Sub
End Class